$(function () {
    function PMPaidBill() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');
        this.exportBtn = $('#exportBtn');
        // 初始化
        this.init();
    }

    PMPaidBill.prototype.Constructor = PMPaidBill;

    PMPaidBill.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    PMPaidBill.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/pmbill/paid/page",
            "aoColumns": [
                {"sWidth": "8%", "sTitle": "房号", "sClass": "center", "mData": "houseNumber", "bSortable": false},
                {"sWidth": "8%", "sTitle": "住户", "sClass": "center", "mData": "userName", "bSortable": false},
                {"sWidth": "10%", "sTitle": "住户手机号", "sClass": "center", "mData": "userMobile", "bSortable": false},
                {"sWidth": "10%", "sTitle": "费用时间", "sClass": "center", "mData": "duration", "bSortable": false},
                {"sWidth": "10%", "sTitle": "出账日期", "sClass": "center", "mData": "releaseTime", "bSortable": false},
                {"sWidth": "10%", "sTitle": "下发时间", "sClass": "center", "mData": "publishTime", "bSortable": false},
                {"sWidth": "8%", "sTitle": "账单金额", "sClass": "center", "mData": "totalAmount", "bSortable": false},
                {"sWidth": "8%", "sTitle": "实收金额", "sClass": "center", "mData": "paidAmount", "bSortable": false},
                {"sWidth": "12%", "sTitle": "缴费订单号", "sClass": "center", "mData": "payOrderNo", "bSortable": false},
                {"sWidth": "10%", "sTitle": "缴费时间", "sClass": "center", "mData": "paidTime", "bSortable": false},
                {"sWidth": "10%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];

                if (aData['releaseTime']) {
                    $('td:eq(4)', nRow).html(SysTool.Date.formatDate(aData['releaseTime'], 'yyyy-MM-dd'));
                }
                var publishTime = aData['publishTime'];
                if (publishTime) {
                    $('td:eq(5)', nRow).html(SysTool.Date.formatDate(aData['publishTime'], 'yyyy-MM-dd'));
                }
                if (aData['paidTime']) {
                    $('td:eq(9)', nRow).html(SysTool.Date.formatDate(aData['paidTime'], 'yyyy-MM-dd'));
                }
                // 操作
                var option = '<a title="账单明细" href="/pmbill/paid/view/' + id + '" class="btn btn-xs btn-info">明细</a>';
                $('td:eq(10)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    PMPaidBill.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    PMPaidBill.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {
            $('#liveUserName').val('');
            $('#liveUserMobile').val('');
            $('#houseNumber').val('');
            $('#releaseStartTime').val('');
            $('#releaseEndTime').val('');
            that.$seachr_btn.click();
        });

        that.exportBtn.click(function (e) {
            that.exportBtn.attr("disabled", true);
            that.exportBtn.html('<i class="icon-plus icon-on-right bigger-120" style="top: 2px"></i>导出中...');
            window.location.href = '/pmbill/paid/export';
            that.exportBtn.removeAttr("disabled");
            that.exportBtn.html('<i class="icon-plus icon-on-right bigger-120" style="top: 2px"></i>导出已缴账单');
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.PMNeedPayBill = new PMPaidBill();
});